package com.example.tsproject.Controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.example.tsproject.Utils.ThreadLocalUtil;
import com.example.tsproject.entity.Students;
import com.example.tsproject.entity.Teachers;
import com.example.tsproject.service.StudentsService;
import com.example.tsproject.entity.Result;
import com.example.tsproject.service.TeachersService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.rmi.server.RemoteRef;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
//token是存在浏览器里面的
@Controller
public class LoginByPasswordController {
    @Autowired
    StudentsService studentsService;

    @Autowired
    TeachersService teachersService;

    @ResponseBody
    @RequestMapping("/StuLoginJudge")
    public Result StuLoginJudge(@RequestParam("student_number") String n,
                             @RequestParam("password") String p){
        Optional<Students> studentsOptional = studentsService.findStudentsJudge(n,p);
        if (studentsOptional.isPresent()) {
            Students students=studentsService.findStudents(n);
            if(students==null)
                return Result.error("无该账号信息");
            System.out.println(students.getStatus());
            System.out.println("AAAAAAAACCCCCCCC");
            if(students.getStatus().equals("inactive"))
                return Result.error("账号已被禁用");
            String token="0";
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.SECOND, 3600);
            Map<String,Object> Claims = new HashMap<>();
            Claims.put("id",students.getId());
            Claims.put("student_number",students.getStudent_number());
            Claims.put("stuclass",students.getStuclass());
            Claims.put("name",students.getName());
            Claims.put("email",students.getEmail());
            Claims.put("avatar",students.getAvatar());
            Claims.put("status",students.getStatus());
            Claims.put("password",students.getPassword());
            token = JWT.create()
                    .withClaim("Claims",Claims)  // Payload"
                    .withExpiresAt(calendar.getTime())
                    .sign(Algorithm.HMAC256("YoungDra"));  // 签名用的secret
            return Result.success(token);
            // 处理找到的用户
        } else {
            // 处理未找到用户的情况
            return Result.error("账号或密码错误");
        }
    }
    @ResponseBody
    @RequestMapping("/TeaLoginJudge")
    public Result TeaLoginJudge(@RequestParam("employee_number") String n,
                             @RequestParam("password") String p){

        Optional<Teachers> teachersOptional = teachersService.findTeachersJudge(n,p);

        Teachers teachers1=(Teachers) ThreadLocalUtil.get();
        System.out.println(teachers1);
        if (teachersOptional.isPresent()) {
            Teachers teachers = teachersService.findTeachers(n);
            String token="";
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.SECOND, 3600);
            Map<String,Object> Claims = new HashMap<>();

            Claims.put("id",teachers.getId());
            Claims.put("employee_number",teachers.getEmployee_number());
            Claims.put("name",teachers.getName());
            Claims.put("email",teachers.getEmail());
            Claims.put("avatar",teachers.getAvatar());
            Claims.put("status",teachers.getStatus());
            Claims.put("password",teachers.getPassword());
            System.out.println(teachers);
            token = JWT.create()
                    .withClaim("Claims",Claims)  // Payload"
                    .withExpiresAt(calendar.getTime())
                    .sign(Algorithm.HMAC256("YoungDra"));  // 签名用的secret
            return Result.success(token);
            // 处理找到的用户
        } else {
            // 处理未找到用户的情况
            return Result.error("账号或密码错误");
        }
    }
}
